<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title></title>
   <%@include file="/WEB-INF/jsp/component/commonHead.jsp" %>
</head>
<body>
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">角色查询</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        角色列表
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">

                        <form class="form-inline" style="display:none" >
                            <div class="form-group">
                                <label for="username">角色名</label>
                                <input type="text" class="form-control" id="" name="" placeholder="角色名称">
                            </div>
                            <button type="submit" class="btn btn-primary">查找</button>
                        </form>
<br/>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="dataTables">
                                <thead>
                                <tr>
                                    <th width="30px" align="center">
									<input type="checkbox" class="group-checkable" id="all_id" />
								</th>
                                    <th>角色名</th>
                                    <th>备注</th>
                                    <th>创建时间</th>
                                    <th>更新时间</th>
                                </tr>
                                </thead>
                                <tbody>
                               
                                </tbody>
                            </table>
                            <div></div>
                        </div>
                        <!-- /.table-responsive -->
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    <!-- /#page-wrapper -->
   <div id="add-modal" class="modal fade" tabindex="-1" data-width="600" data-backdrop="true" ></div>
   <div id="update-modal" class="modal fade" tabindex="-1" data-width="600"></div>
   <div id="permission" class="modal fade" tabindex="-1" data-width="600"></div>
   <div id="permission2users" class="modal fade" tabindex="-1" data-width="600"></div>
<%@include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
    <!-- DataTables JavaScript -->
    <script src="${js}/jquery.twbsPagination.min.js"></script>
    <script>
    var totalPages;
	var startPage;
	$(document).ready(function(){
    	$("#yhgl").click();
    	initTable();
    	selectData();
    	$("#all_id").click(function(){ 
    		var bischecked = $('#all_id').is(':checked');
    		if(bischecked){
    		    $("[name='itemId']").prop("checked",'true');//全选
    		    $("[name='itemId']").parents('tr').addClass("active");
    		}else{
    			$("[name='itemId']").removeAttr("checked");
    			$("[name='itemId']").parents('tr').removeClass("active");
    		}
    	});
    });
   
	function changbox(obj){
		$(obj).parents('tr').toggleClass("active");
	}
	
    function initTable(){
    	$('#dataTables').dataTable({
    		"dom": "<'row'<'col-xs-2'l><'#mytool.col-xs-10'><'col-xs-6'f>r>" +
            "t" +
            "<'row'<'col-xs-6'i><'col-xs-6'p>>",
    		    "bInfo": true, //页脚信息
    		    "bPaginate":true, //分页
    		    "bFilter": false, //过滤功能
    			"aoColumns":[
					{ "mData": "id" ,
					  "orderable": false,
					  "sDefaultContent" : "",
					  "mRender": function(data, type, full){
		                	return '<input onclick="changbox(this)" type="checkbox" name="itemId" style="margin-left:9px;"  value='+data+' />';
		                }
					},
					{ "mData": "roleName" ,
					  "orderable": false,
					  "sDefaultContent" : ""
					},
					{ "mData": "description",
		              "orderable": false ,
		              "sDefaultContent" : ""
					},
					{ "mData": "createdTimeStr" ,
		               "orderable": false,
		               "sDefaultContent" : ""
		            },
					{ "mData": "updatedTimeStr",
		               "orderable": false,
		               "sDefaultContent" : ""
					}
				],
				"oLanguage" : {
                    "sProcessing" : "正在加载中......",
                    "sLengthMenu" : "每页显示 _MENU_ 条记录",
                    "sZeroRecords" : "没有数据！",
                    "sEmptyTable" : "无数据存在！",
                    "sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                    "sInfoEmpty" : "显示0到0条记录",
                    "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
                    //"sSearch" : "搜索",
                    "oPaginate" : {
                        "sFirst" : "首页",
                        "sPrevious" : "上一页",
                        "sNext" : "下一页",
                        "sLast" : "末页"
                    }
                },
                "aaSorting" : [[0, "desc"]],
				initComplete: function () {
					$("#mytool").append('<shiro:hasPermission name="+user+4">'+
                    		'<button id="permission2users_butt" type="button" class="btn btn-outline btn-success" style="float:right;">分配用户</button>'
                    		+'</shiro:hasPermission>');
					 $("#mytool").append('<shiro:hasPermission name="+user+4">'+
	                    		'<button id="permission_butt" type="button" class="btn btn-outline btn-success" style="float:right;">权限</button>'
	                    		+'</shiro:hasPermission>');
                    $("#mytool").append(
                    		'<shiro:hasPermission name="+user+8">'+
                    		'<button id="delete_butt" type="button" class="btn btn-outline btn-success" style="float:right;">删除</button>'
                    		+'</shiro:hasPermission>');
                    $("#mytool").append('<shiro:hasPermission name="+user+4">'+
                    		'<button id="update_butt" type="button" class="btn btn-outline btn-success" style="float:right;">修改</button>'
                    		+'</shiro:hasPermission>');
                    $("#mytool").append('<shiro:hasPermission name="+user+2">'+
                    		'<button id="add_butt" type="button" class="btn btn-outline btn-success" style="float:right;">新增</button>'
                    		+'</shiro:hasPermission>');
                    $("#add_butt").on("click", toAdd);
                    $("#update_butt").on("click", toUpdate);
                    $("#delete_butt").on("click", toDelete);
                    $("#permission_butt").on("click", showPermission);
                    $("#permission2users_butt").on("click", permission2users);
                }
   	 });
    }
    
    function selectData(){
    	var ajaxUrl = "${ctx}/admin/mgmt/role/search";
      	$.ajax({
    	    type:"post",
    	    url:ajaxUrl,
    	    data:{
    	    },
    	    dataType: "json",
    	    success:function(json){
    	    	//var da = JSON.stringify(json);
    	    	//$("#content").html(da);
    	    	 $('#dataTables').dataTable().fnClearTable(); 
    	    	 $('#dataTables').dataTable().fnAddData(json,true);
    	    },
    	    complete:function () {
                //修改iframe高度
                reSizeParentIframe();
            }
    	});
    }
    
    //var addModal = $('#add-modal');
    var addModal = window.parent.$("#dialog-Show");
    function toAdd(){
    	setTimeout(function(){
    		addModal.load('${ctx}/admin/mgmt/role/init?obj=add','',function(){
    			addModal.modal();
    		});
    	},500);
    }
    
    //var addModal2 = $('#update-modal');
    var addModal2 = window.parent.$("#dialog-Show");
    function toUpdate(){
    	var ids = new Array();
    	$('input[name="itemId"]:checked').each(function(){
            var id=$(this).val();
            ids.push(id);
         });
    	if(ids.length==0){
    		window.parent.bootbox.alert("请先选择要修改的角色信息！");
    		return false;
    	}else{
    		var url="${ctx}/admin/mgmt/role/init?obj=edit&ids="+ids;
    		setTimeout(function(){
    			addModal2.load(url,'',function(){
    				addModal2.modal();
    			});
    		},500);
    	}
    }
    
    function toDelete(){
    	var ids = new Array();
    	$('input[name="itemId"]:checked').each(function(){
            var id=$(this).val();
            ids.push(id);
         });
    	if(ids.length==0){
    		window.parent.bootbox.alert("请先选择要删除的角色信息！");
    		return false;
    	}else{
    		var url="${ctx}/admin/mgmt/role/del?obj=del&ids="+ids;
    		window.parent.bootbox.setLocale("zh_CN");
    		window.parent.bootbox.confirm("删除后不可恢复！!",function(result){
    			if(result){
    				$.ajax({
    					type:"post",
    					url:url,
    					dataType:"json",
    				    success:function(result){
    				    	selectData();
    				    },
    				    error:function(data){
    				    	
    				    }
    				});
    			}	
    	  });
    	}
    }
    
    //新建保存(该方法废弃)
    function SaveRole(){
    	var roleName = $("#add_roleName").val();
    	if(roleName==""||roleName==null){
    		bootbox.setLocale("zh_CN");
    		bootbox.alert("请填写角色名称！");
    	}else{
    		var url="${ctx}/admin/mgmt/role/save?operate=add";
        	$.ajax({
        		type:"post",
        		url:url,
        		dataType:"html",
        	    data:$("#addForm").serialize(),
        	    success:function(result){
        	    	$("#add-modal").modal('hide');
        	    	selectData();
        	    },
        	    error:function(data){
        	    	
        	    }
        	});
    	}
    }
    
    //更新保存(该方法废弃)
    function updateRole(){
    	var roleName = $("#edit_roleName").val();
    	if(roleName==""||roleName==null){
    		bootbox.setLocale("zh_CN");
    		bootbox.alert("请填写角色名称！");
    	}else{
    		var url="${ctx}/admin/mgmt/role/save?operate=edit";
        	$.ajax({
        		type:"post",
        		url:url,
        		dataType:"html",
        	    data:$("#updateForm").serialize(),
        	    success:function(result){
        	    	$("#update-modal").modal('hide');
        	    	selectData();
        	    },
        	    error:function(data){
        	    	
        	    }
        	});
    	}
    }
    
    //权限树列表
    //var addModal3 = $('#permission');
    var addModal3 = window.parent.$("#dialog-Show");
    function showPermission(){
    	var ids = new Array();
    	$('input[name="itemId"]:checked').each(function(){
            var id=$(this).val();
            ids.push(id);
         });
    	if(ids.length==0){
    		window.parent.bootbox.alert("请先选择要修改权限的角色信息！");
    		return false;
    	}else{
    		var url = "${ctx}/admin/mgmt/role/"+ids[0];
    		setTimeout(function(){
        		addModal3.load(url,'',function(){
        			addModal3.modal();
        		});
        	},500);
    	}
    }
    
    //分配用户
    //var addModal4 = $('#permission2users');
    var addModal4 = window.parent.$("#dialog-Show");
    function permission2users(){
    	var ids = new Array();
    	$('input[name="itemId"]:checked').each(function(){
            var id=$(this).val();
            ids.push(id);
         });
    	if(ids.length==0){
    		window.parent.bootbox.alert("请选择一个的角色!");
    		return false;
    	}else{
    		var url = "${ctx}/admin/mgmt/role/permissions2users?roleId="+ids[0];
    		setTimeout(function(){
        		addModal4.load(url,'',function(){
        			addModal4.modal();
        		});
        	},500);
    	}
    }
    
    //保存用户分配信息(废弃)
    function saveUserRoleAsso(){
    	var userIds = $("#my_multi_select1").val();
    	var ids = new Array;
    	if(userIds!=null&&userIds!=""){
    	   ids=(userIds+"").split(",");
    	}
    	var roleId = $("#roleId").val();
    	var url = "${ctx}/admin/mgmt/role/saveUserRoleAsso?userIds="+ids;
    	$.ajax({
    		type:"post",
    		url:url,
    		dataType:"json",
    	    data:{
    	    	'roleId':roleId
    	    },
    	    success:function(result){
    	    	$("#permission2users").modal('hide');
    	    	//selectData();
    	    },
    	    error:function(data){
    	    	
    	    }
    	});
    }
    </script>
</body>
</html>